package cn.client.ui.view.main.mindfulness

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import cn.client.data.remoteData.bean.DateUtils
import cn.client.data.remoteData.bean.PageSelectByUid
import cn.client.data.userStateManager
import kotlinx.coroutines.launch


// 打卡列表
@Composable
fun MindfulnessLog(
    viewModel: MindfulnessViewModel
) {

    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    val mindfulnessLogState = viewModel.mindfulnessLog.collectAsState(initial = emptyList())
    val mindfulnessLog = mindfulnessLogState.value

    val pageSelectByUid = PageSelectByUid(uid = userId, pageNo = 1, pageSize = 10)

    // 初始化获取记录列表
    viewModel.dispatch(MindfulnessViewModel.PageIntent.GetLogList(pageSelectByUid))

    Column(
        modifier = Modifier.fillMaxSize()
    ) {

        LazyColumn(
            modifier = Modifier.fillMaxWidth().padding(5.dp)
        ){
            items(mindfulnessLog){ it ->
                MindfulnessLogItem(
                    logInfo = it.experience,
                    logDate = it.gmtCreate
                )
            }
        }
    }
}


// 打卡记录item
@Composable
fun MindfulnessLogItem(
    logInfo: String,
    logDate: Long
){
    val createDate: String = DateUtils.dateToStr(logDate)
    var isExpanded by remember { mutableStateOf(false) }


    Column(
        modifier = Modifier
    ) {
        Row(
            horizontalArrangement = Arrangement.Start,
            modifier = Modifier
                .padding(5.dp)
                .fillMaxWidth()
        ) {
            Text(
                text = logInfo, fontSize = 16.sp,
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable {
                        isExpanded = !isExpanded
                    },
                maxLines = if (isExpanded) Int.MAX_VALUE else 3,
            )
        }
        Row(
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier
                .padding(5.dp)
                .fillMaxWidth()
        ) {
            Text(
                text = createDate, color = Color.Gray,
                fontSize = 12.sp
            )
        }
    }

}